﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using Sio.Mdm.Entities;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.BF.Administracija;
using Sio.Mdm.BF.Ldap;
using Sio.Mdm.Types.Database;
using Sio.Mdm.Types;
using System.Collections.Generic;

namespace Sio.Mdm.Web.Instalacija
{
	public partial class ZdrDomoviSeznam : Controls.Page
	{
		#region Definitions
		public interface OrgRowEditData
		{
			int ZdravstveniDomId { get; set; }
			string Naziv { get; set; }
			int? ObcinaId { get; set; }
		}
		#endregion

		#region OnLoad
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!IsPostBack && !X.IsAjaxRequest)
			{
				//občine
				EntityCollection<Obcina> obcine = new EntityCollection<Obcina>(
				new Filter<Obcina>().Where(o => o.ObcinaId != null).OrderBy(o => o.Naziv));
				this.StoreObcine.DataSource = obcine;
				this.StoreObcine.DataBind();
				NaloziSeznamZapisov();
			}
		}
		#endregion

		#region Editing
		protected void UrediPodatke(Object sender, DirectEventArgs e)
		{
			OrgRowEditData EditData = XML.Deserialize<OrgRowEditData>(e.ExtraParams["data"]);
			if (EditData.ZdravstveniDomId == -99)
			{
				ZdravstveniDom ent = EntityFactory.CreateInstance<ZdravstveniDom>();
				ent.Naziv = EditData.Naziv;
				ent.ObcinaId = EditData.ObcinaId;

				ent.Update();
			}
			else
			{
				ZdravstveniDom ent = EntityFactory.LoadEntity<ZdravstveniDom>
				(new Filter<ZdravstveniDom>().Where(r => r.ZdravstveniDomId == EditData.ZdravstveniDomId)
				);
				if (ent.EntityState != EntityState.Unknown)
				{
					ent.Naziv = EditData.Naziv;
					ent.ObcinaId = EditData.ObcinaId;

					ent.Update();
				}
				else
				{
					e.Success = false;
					X.Msg.Alert("Manjkajoči podatki", "Iskani zapis ne obstaja.").Show();
					return;
				}
			}
		}
		protected void OdstraniZapise(object sender, DirectEventArgs e)
		{
			List<int> deletedItems = e.GetIdList<int>("selected", "ZdravstveniDomId");
			if (deletedItems.Count > 0)
			{
				EntityCollection<ZdravstveniDom> col = new EntityCollection<ZdravstveniDom>(
					new Filter<ZdravstveniDom>().SelectByField(r => r.ZdravstveniDomId, deletedItems.ToArray())
				);
				col.RemoveAll();
				col.Update();
			}
		}
		#endregion

		#region Private
		private void NaloziSeznamZapisov()
		{
			GridZdrDomoviSeznam.LoadData<ZdravstveniDom>(
					new Filter<ZdravstveniDom>().Where(p => p.ZdravstveniDomId != null).OrderBy(p => p.Naziv));
		}
		#endregion
	}
}
